package com.mk.admin.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.mk.admin.mapper.MessageReadMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.entity.MessageReadEntity;
import com.mk.admin.model.vo.MessageReadVo;
import com.mk.admin.service.MessageReadService;
import com.mk.admin.util.DateTool;
import com.mk.admin.util.JSONCopy;
import com.mk.admin.util.StringTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MessageReadServiceImpl implements MessageReadService {
    @Autowired
    private MessageReadMapper messageReadMapper;

    @Override
    public ResultDto select(MessageReadVo messageReadVo) {
        MessageReadEntity entity = JSONCopy.copy(messageReadVo, MessageReadEntity.class);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("count", messageReadMapper.count(entity));
        jsonObject.put("data", messageReadMapper.select(entity));
//        jsonObject.put("notReadCountPat", messageReadMapper.notReadCountPat(entity));
        return ResultDto.data(jsonObject);
    }

    @Override
    public ResultDto selectNotReadPat(MessageReadVo vo) {
        MessageReadEntity entity = JSONCopy.copy(vo, MessageReadEntity.class);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("count", messageReadMapper.notReadCountPat(entity));
        jsonObject.put("data", messageReadMapper.selectNotReadPat(entity));
        return ResultDto.data(jsonObject);
    }

    @Override
    public ResultDto selectNotReadCountPat(MessageReadVo vo) {
        MessageReadEntity entity = JSONCopy.copy(vo, MessageReadEntity.class);
        return ResultDto.data(messageReadMapper.notReadCountPat(entity));
    }

    @Override
    public ResultDto addBatch(List<MessageReadVo> list) {
        list.forEach(item -> {
            if (item.getId() == null) {
                MessageReadEntity entity = JSONCopy.copy(item, MessageReadEntity.class);
                entity.setReadTime(DateTool.format());
                entity.setReadStatus(1);
                messageReadMapper.insert(entity);
            } else {
                MessageReadEntity entity = new MessageReadEntity();
                entity.setReadStatus(1);
                entity.setId(item.getId());
                messageReadMapper.updateById(entity);
            }
        });
        return ResultDto.data("success");
    }

    @Override
    public ResultDto add(MessageReadVo messageReadVo) {
        messageReadVo.setReadTime(DateTool.format());
        return ResultDto.data(messageReadMapper.insert(JSONCopy.copy(messageReadVo, MessageReadEntity.class)));
    }

    @Override
    public ResultDto update(MessageReadVo messageReadVo) {
        return ResultDto.data(messageReadMapper.updateById(JSONCopy.copy(messageReadVo, MessageReadEntity.class)));
    }

    @Override
    public ResultDto delete(int id) {
        return ResultDto.data(messageReadMapper.deleteById(id));
    }

    @Override
    public ResultDto status(int id) {
        return ResultDto.data(messageReadMapper.status(id));
    }


}
